import 'package:ecy_app_flutter/ecycommon/utils/EcyRandomUtil.dart';
import 'package:flutter/material.dart';
import 'package:ecy_app_flutter/routes/blind_box/exchange_market/models/exchange_user_group.dart';
import 'package:ecy_app_flutter/routes/blind_box/exchange_market/models/exchange_card.dart';

class ExchangeRequestListPage extends StatelessWidget {
  // 模拟数据
  List<ExchangeUserGroup> requests = [ExchangeUserGroup(userName: "userName", userAvatar: EcyRandomUtil.randomIconUrl(), 
  offerCards: [ExchangeCard(id: 0, name: "1", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: true)], 
  wantCards: [ExchangeCard(id: 1, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false),ExchangeCard(id: 2, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false)],
   canExchange: null),
   ExchangeUserGroup(userName: "userName", userAvatar: EcyRandomUtil.randomIconUrl(), 
  offerCards: [ExchangeCard(id: 0, name: "1", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: true)], 
  wantCards: [ExchangeCard(id: 1, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false),ExchangeCard(id: 2, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false)],
   canExchange: true),ExchangeUserGroup(userName: "userName", userAvatar: EcyRandomUtil.randomIconUrl(), 
  offerCards: [ExchangeCard(id: 0, name: "1", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: true)], 
  wantCards: [ExchangeCard(id: 1, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false),ExchangeCard(id: 2, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false)],
   canExchange: null),
   ExchangeUserGroup(userName: "userName", userAvatar: EcyRandomUtil.randomIconUrl(), 
  offerCards: [ExchangeCard(id: 0, name: "1", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: true)], 
  wantCards: [ExchangeCard(id: 1, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false),ExchangeCard(id: 2, name: "123", imageUrl: EcyRandomUtil.randomIconUrl(), expiryTime: null, owned: false)],
   canExchange: true)];

  ExchangeRequestListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('互换申请'),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      backgroundColor: const Color(0xFFF5F5F5),
      body: ListView.builder(
        padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 12),
        itemCount: requests.length,
        itemBuilder: (context, index) {
          final group = requests[index];
          int status = group.canExchange == null ? 0 : (group.canExchange! ? 1 : 2);
          String statusText;
          Color statusColor;
          switch (status) {
            case 1:
              statusText = "已同意与对方交换";
              statusColor = const Color(0xFFBDBDBD);
              break;
            case 2:
              statusText = "已拒绝与对方交换";
              statusColor = const Color(0xFFBDBDBD);
              break;
            default:
              statusText = "";
              statusColor = Colors.transparent;
          }
          return Card(
            elevation: 0,
            margin: const EdgeInsets.only(bottom: 16),
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
            color: const Color(0xFF5368F7).withOpacity(0.08),
            child: Padding(
              padding: const EdgeInsets.all(16),
              child: Column(
                children: [
                  // 头像行
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      CircleAvatar(
                        backgroundImage: NetworkImage(group.userAvatar ?? ''),
                        radius: 22,
                      ),
                      const SizedBox(width: 96),
                      CircleAvatar(
                        backgroundImage: NetworkImage(group.userAvatar ?? ''),
                        radius: 22,
                      ),
                    ],
                  ),
                  const SizedBox(height: 8),
                  // 白色内容框
                  Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(16),
                    ),
                    padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            // ta想用
                            Column(
                              children: [
                                const Text('ta想用', style: TextStyle(fontSize: 12, color: Color(0xFF262626))),
                                const SizedBox(height: 4),
                                _buildCardItem(group.offerCards.first, owned: true),
                              ],
                            ),
                            const Padding(
                              padding: EdgeInsets.symmetric(horizontal: 25),
                              child: Icon(Icons.swap_horiz, color: Color(0xFFF62EBD), size: 32),
                            ),
                            // 与我交换
                            Column(
                              children: [
                                const Text('与我交换', style: TextStyle(fontSize: 12, color: Color(0xFF262626))),
                                const SizedBox(height: 4),
                                _buildCardItem(group.wantCards.first, owned: false),
                              ],
                            ),
                          ],
                        ),
                        const SizedBox(height: 12),
                        if (status == 0)
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              OutlinedButton(
                                onPressed: () {
                                  _showRejectDialog(context);
                                },
                                style: OutlinedButton.styleFrom(
                                  foregroundColor: const Color(0xFFF62EBD),
                                  side: const BorderSide(color: Color(0xFFF62EBD)),
                                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
                                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 10),
                                ),
                                child: const Text('拒绝'),
                              ),
                              const SizedBox(width: 16),
                              ElevatedButton(
                                onPressed: () {
                                  _showAgreeDialog(context);
                                },
                                style: ElevatedButton.styleFrom(
                                  backgroundColor: const Color(0xFFF62EBD),
                                  foregroundColor: Colors.white,
                                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
                                  padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 10),
                                ),
                                child: const Text('同意'),
                              ),
                            ],
                          )
                        else
                          Text(
                            statusText,
                            style: TextStyle(fontSize: 13, color: statusColor),
                            textAlign: TextAlign.center,
                          ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

   void _showAgreeDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        child: Container(
          width: 280,
          padding: const EdgeInsets.only(top: 24, left: 16, right: 16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                '交换完成不可撤销，双方物品将直接转移，确定要交换吗？',
                style: TextStyle(fontSize: 15, color: Colors.black87, fontWeight: FontWeight.w600),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 24),
              const Divider(height: 1, thickness: 1),
              SizedBox(
                height: 48,
                child: Row(
                  children: [
                    Expanded(
                      child: TextButton(
                        style: TextButton.styleFrom(
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(16),
                            ),
                          ),
                        ),
                        onPressed: () => Navigator.of(context).pop(),
                        child: const Text('我再想想', style: TextStyle(color: Colors.black, fontSize: 16)),
                      ),
                    ),
                    Container(
                      width: 1,
                      height: double.infinity,
                      color: Colors.grey[300],
                    ),
                    Expanded(
                      child: TextButton(
                        style: TextButton.styleFrom(
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                              bottomRight: Radius.circular(16),
                            ),
                          ),
                        ),
                        onPressed: () {
                          Navigator.of(context).pop();
                          // TODO: 处理同意逻辑
                        },
                        child: const Text('同意交换', style: TextStyle(color: Color(0xFFF62EBD), fontSize: 16, fontWeight: FontWeight.w600)),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _showRejectDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        child: Container(
          width: 280,
          padding: const EdgeInsets.only(top: 24, left: 16, right: 16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                '确定要拒绝交换吗？',
                style: TextStyle(fontSize: 15, color: Colors.black87, fontWeight: FontWeight.w600),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 24),
              const Divider(height: 1, thickness: 1),
              SizedBox(
                height: 48,
                child: Row(
                  children: [
                    Expanded(
                      child: TextButton(
                        style: TextButton.styleFrom(
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(16),
                            ),
                          ),
                        ),
                        onPressed: () => Navigator.of(context).pop(),
                        child: const Text('我再想想', style: TextStyle(color: Colors.black, fontSize: 16)),
                      ),
                    ),
                    Container(
                      width: 1,
                      height: double.infinity,
                      color: Colors.grey[300],
                    ),
                    Expanded(
                      child: TextButton(
                        style: TextButton.styleFrom(
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(
                              bottomRight: Radius.circular(16),
                            ),
                          ),
                        ),
                        onPressed: () {
                          Navigator.of(context).pop();
                          // TODO: 处理拒绝逻辑
                        },
                        child: const Text('确定拒绝', style: TextStyle(color: Color(0xFFF62EBD), fontSize: 16, fontWeight: FontWeight.w600)),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
 
  static Widget _buildCardItem(ExchangeCard card, {bool owned = false}) {
    return Column(
      children: [
        Image.network(card.imageUrl ?? '', width: 50, height: 59),
        const SizedBox(height: 4),
        Text(card.name, style: const TextStyle(fontSize: 12, color: Color(0xFF262626))),
      ],
    );
  }
} 